package com.happyscan138.makefun.Fragment;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.happyscan138.makefun.R;
import com.happyscan138.makefun.adapter.MyFragmentPagerAdapter;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/22.
 */

public class battle_Fragment extends Fragment {
    private ViewPager mPager; //定义viewPager
    private ImageView ivBottomLine;// 横向下划线
    private  Fragment newFragment,goodFragment,hotFragment;
    private ArrayList<Fragment> list;//定义要装fragment的列表
    private RadioButton buttonNew,buttonGood,buttonHot;
    private int pWidth;
    private int offset ;// 图片偏移量
    private final static int num = 5 ;
    private int position_one;
    private int currIndex = 0;
    int section;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.battlefragment,container,false);
        InitWidth(view);
        InitTextView(view);
        InitViewPager(view);
        return view;
    }

    private void InitTextView(View parentView) {
        mPager = (ViewPager) parentView.findViewById(R.id.battle_viewpager);
        ivBottomLine = (ImageView)parentView.findViewById(R.id.battle_bottom_line);
        buttonNew = (RadioButton) parentView.findViewById(R.id.battle_new);
        buttonHot = (RadioButton) parentView.findViewById(R.id.battle_hot);
        buttonGood = (RadioButton) parentView.findViewById(R.id.battle_good);

        buttonNew.setOnClickListener(new MyOnClickListener(0));
        buttonGood.setOnClickListener(new MyOnClickListener(1));
        buttonHot.setOnClickListener(new MyOnClickListener(2));

    }
    private void InitViewPager(View parentView) {
        list = new ArrayList<Fragment>();

        newFragment = new battle_new();
        goodFragment = new battle_good();
        hotFragment= new battle_hot();

        list.add(newFragment);
        list.add(goodFragment);
        list.add(hotFragment);

        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), list));
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Animation animation = null;
                switch (position) {
                    case 0:
                        if (position== 0) {
                            animation = new TranslateAnimation(section * currIndex , section * position, 0, 0);
                        }
                        break;
                    case 1:
                        if (position == 1) {
                           animation = new TranslateAnimation(section * currIndex, section * position, 0, 0);

                        }
                        break;
                    case 2:
                        if (position == 2) {
                            animation = new TranslateAnimation(section * currIndex, section * position, 0, 0);}
                        break;
                }
                currIndex = position;
                animation.setFillAfter(true);
                animation.setDuration(300);
                ivBottomLine.startAnimation(animation);
            }
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
    private void InitWidth(View parenView){
        ivBottomLine = (ImageView) parenView.findViewById(R.id.battle_bottom_line);
        BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.mipmap.up_tab);
        Bitmap bmp = bd.getBitmap();
        pWidth = ivBottomLine.getLayoutParams().width;
        pWidth = bmp.getWidth();
        // 获取手机屏幕分辨率(宽度)
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;

        // 每一个标签页所占：offset+pWidth+offset...[以此类推]
        offset = (screenW /num -pWidth)/2 ;//移动偏移量
        int avg = (int) (screenW / num);
        section = offset*2+pWidth ;
        int position_one=offset+avg;
        // 通过LayoutParams.setMargins属性设置偏移量
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);  // , 1是可选写的
        lp.setMargins(position_one, 0, 0, 0);
        ivBottomLine.setLayoutParams(lp);

    }
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };

}
